package com.wu.dms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wu.dms.cache.UrlCache;
import com.wu.common.convertion.result.Result;
import com.wu.common.convertion.result.Results;
import com.wu.constant.Enum.MinIoNameEnum;
import com.wu.dms.dao.entity.Singer;
import com.wu.dms.dao.mapper.SingerMapper;
import com.wu.dms.dto.SingerRequestDTO;
import com.wu.dms.dto.resp.PageResp;
import com.wu.dms.service.SingerService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author EDY
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/singer")
public class SingerController {


    private final SingerService actorService;

    private final SingerMapper singerMapper;


    // 添加歌手
    @PostMapping("/add")
    public Result<Boolean> addSinger(@RequestBody SingerRequestDTO requestParam) {
        actorService.addSinger(requestParam);
        return Results.success(true);
    }

    // 删除歌手
    @DeleteMapping("/delete")
    public Result<Boolean> deleteSinger(@RequestParam Long id) {
        actorService.deleteSinger(id);
        return Results.success(true);
    }


    // 更新歌手信息
    @PostMapping("/update")
    public Result<Boolean> updateSingerMsg(@RequestBody SingerRequestDTO updateSingerRequestDTO) {
        actorService.updateSingerMsg(updateSingerRequestDTO);
        return Results.success(true);
    }

    // 更新歌手头像
    @PostMapping("/avatar/update")
    public Result<Boolean> updateSingerPic(@RequestParam("file") MultipartFile avatarFile, @RequestParam("id") Long id) {
        actorService.updateSingerPic(avatarFile, id);
        return Results.success(true);
    }
    @GetMapping
    public Result<PageResp> querySingerInPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize, @RequestParam String singerName) {
        Page<Singer> page = new Page<>(pageNum, pageSize);
        Page<Singer> singerPage = actorService.query().like(singerName!=null,"name",singerName).page(page);
        List<Singer> singerList = singerPage.getRecords();
        long total = singerPage.getTotal();
         singerList.forEach(singer -> {
                    String url = UrlCache.getUrl(MinIoNameEnum.SINGER_IMAGE.getBucketName(), singer.getId(),singer.getPic());
                    singer.setPic(url);
                });
        PageResp pageResp =new PageResp();
        pageResp.setData(singerList);
        pageResp.setTotal(total);
        return Results.success(pageResp);
    }
}
